package cn.edu.nit.sys.service.impl;

import cn.edu.nit.sys.mapper.ReportMapper;
import cn.edu.nit.sys.service.ReportService;
import cn.edu.nit.sys.utils.R;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.UUID;

/**
 * @Author 罗文斌
 * @Date 2023/4/16
 * @Version 1.0.0
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public R addReport(Map<String, String> params) {
        String id = UUID.randomUUID().toString().replaceAll("\\-", "");
        String content = "";
        String type = params.get("type");
        String userId = params.get("userId");
        String reportId = params.get("reportId");
        if("1".equals(type)) {
            content = params.get("radio");
        }else if("0".equals(type)) {
            content = params.get("content");
        }
        if(StringUtils.isNotEmpty(reportMapper.isExist(userId, reportId, content))) {
            return R.error("请勿重复举报");
        }
        return reportMapper.addReport(id, userId, type, content, reportId) > 0 ? R.ok("举报成功") : R.error("举报失败");
    }

    @Override
    public R getList(Page<Map<String, String>> page, String siftName) {
        Page<Map> list = reportMapper.getList(page, siftName);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", list.getTotal());
        jsonObject.put("list", list.getRecords());
        return R.ok(jsonObject);
    }

    @Override
    @Transactional
    public R addBan(Map<String, String> params) {
        String id = UUID.randomUUID().toString().replaceAll("\\-", "");
        String userId = params.get("userId");
        String type = params.get("type");
        String start = params.get("start");
        String end = params.get("end");
        int rs = reportMapper.updateStatus(userId);
        rs += reportMapper.addBan(id, userId, type, start, end);
        return rs > 0 ? R.ok("操作成功") : R.error("操作失败");
    }
}
